package com.song.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.song.common.enums.BusinessType;
import com.song.common.result.R;
import com.song.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.song.framework.annotation.RateLimiter;
import com.song.module.admin.entity.ApCookbook;
import com.song.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.song.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.service.IApCookbookService;
import com.song.module.admin.request.ApCookbookRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 菜谱控制层
 *
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "菜谱接口管理",tags = "菜谱接口管理")
@RequestMapping("/admin/apCookbook" )
public class ApCookbookController {

    private final IApCookbookService iApCookbookService;

    /**
     * 分页查询菜谱列表
     */
    @ApiOperation(value = "分页查询菜谱列表")
    @PostMapping("/pageList")
    public R<PageInfo<ApCookbook>> pageList(@RequestBody ApCookbookRequest apCookbookRequest) {
        return R.success(iApCookbookService.pageList(apCookbookRequest));
    }

    /**
     * 获取菜谱详细信息
     */
    @ApiOperation(value = "获取菜谱详细信息")
    @GetMapping(value = "/query/{id}")
    public R<ApCookbook> detail(@PathVariable("id") Long id) {
        return R.success(iApCookbookService.getById(id));
    }

    /**
     * 新增菜谱
     */
    @Log(title = "新增菜谱", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增菜谱")
    @PostMapping("/add")
    public R<Void> add(@RequestBody ApCookbookRequest apCookbookRequest) {
        iApCookbookService.add(apCookbookRequest);
        return R.success();
    }

    /**
     * 修改菜谱
     */
    @Log(title = "修改菜谱", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改菜谱")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody ApCookbookRequest apCookbookRequest) {
        iApCookbookService.update(apCookbookRequest);
        return R.success();
    }

    /**
     * 删除菜谱
     */
    @Log(title = "删除菜谱", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除菜谱")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iApCookbookService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出菜谱
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody ApCookbookRequest apCookbookRequest) throws IOException {
        List<ApCookbook> list = iApCookbookService.list(apCookbookRequest);
        ExcelUtils.exportExcel(list, ApCookbook.class, "菜谱信息", response);
    }
}
